Imports System.Data.SqlClient
Imports System.Data
Imports CAPA_Entidad
Imports CAPA_Utilitarios
Public Class DocumentoClienteDetalleDatos

    'Se crea una instancia unica para las conexiones
    Dim conex As Conexion = Conexion.getInstance()

    Function listarDetallePedido(ByVal codCIA As String, ByVal codSUC As String, ByVal tdoc As String, ByVal codDCA As String, ByVal codDAP As String, ByVal codUbicacion As String, ByVal codArticulo As String, ByVal flag As String) As DataSet
        Dim ds As New DataSet
        Dim con As SqlConnection = conex.obtenerConexion()
        Try
            con.Open()

            Dim query As String = "SELECT ART_Codigo AS Item,ART_Descripcion AS Articulo,ART_UM AS Unidad,UBI_Descripcion AS Ubicacion, " & _
                                  "DADPU_Cantidad AS Cantidad, DADPU_CantPick AS CantPick, UBI_Codigo, DADPU_Codigo, DADP_Codigo " & _
                                  "FROM VW_ListarDetallePedidoNoCombo " & _
                                  "WHERE CIA = @CIA AND SUC_Codigo = @SUC " & _
                                  "AND TDOC_Codigo = @TDOC AND DCA_Codigo = @DCA AND DAP_Codigo = @DAP " & IIf(codUbicacion = "", "", "AND CIA + SUC_Codigo + ALM_Codigo + UBI_Codigo = @UBI ") & _
                                  IIf(codArticulo = "", "", "AND ART_Codigo = @ART ") & _
                                  IIf(flag = "", "", "AND DADPU_Cantidad - DADPU_CantPick > 0 ") & _
                                  "ORDER BY DADPU_FechaFin, UBI_Piso DESC, UBI_Pasillo, UBI_Columna, UBI_Fila"

            Dim comando As New SqlCommand(query, con)
            With comando
                .Parameters.AddWithValue("@CIA", codCIA)
                .Parameters.AddWithValue("@SUC", codSUC)
                .Parameters.AddWithValue("@TDOC", tdoc)
                .Parameters.AddWithValue("@DCA", codDCA)
                .Parameters.AddWithValue("@DAP", codDAP)
                If codUbicacion <> "" Then
                    .Parameters.AddWithValue("@UBI", codUbicacion)
                End If
                If codArticulo <> "" Then
                    .Parameters.AddWithValue("@ART", codArticulo)
                End If
            End With
            Dim adapter As New SqlDataAdapter(comando)
            adapter.Fill(ds, "detalles")
        Catch ex As Exception
            ds = Nothing
        Finally
            con.Close()
        End Try

        Return ds
    End Function

End Class
